package com.skyworth.web.admin.category.controller;

import com.google.gson.JsonObject;
import com.skyworth.web.admin.category.domain.CategoryModel;
import com.skyworth.web.admin.category.form.CategoryForm;
import com.skyworth.web.admin.category.service.CategoryService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.Constants;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.common.form.MenuCss;
import com.skyworth.web.common.form.Paginator;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by skyworth on 2016/8/11.
 */
@Controller
@RequestMapping("/admin/category")
public class CategoryController extends BaseController {
	Logger LOG = LoggerFactory.getLogger(CategoryController.class);

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private MenuCss menuCss;


	@RequiresRoles(value = {Constants.CATEGORY_ROLE})
	@RequestMapping("")
	public String index(Paginator paginator, Model model) {
		UserModel userModel = currentUser();
		long store_id = 0;
		JsonObject params = new JsonObject();
		if (userModel != null) {
			store_id = userModel.storeId;
		}
		params.addProperty("store_id", store_id);
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouCategory = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		Page<CategoryModel> page = categoryService.pageCategory(params, paginator.pageRequest());
		paginator.feedToModel(page, model);
		return "admin/category/categories";
	}

	@RequestMapping(value = {"new"})
	public String newAction(Model model) {
		CategoryForm categoryForm = new CategoryForm();
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouCategory = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", currentUser());
		model.addAttribute("item", categoryForm);
		return "admin/category/category_new";
	}


	@RequestMapping("{id}")
	public String edit(@PathVariable("id") long id, Model model) {

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouCategory = "hover";

		model.addAttribute("menuCss", menuCss);

		CategoryModel categoryModel = categoryService.findCategory(id);

		model.addAttribute("category", categoryModel);
		model.addAttribute("user", currentUser());

		return "admin/category/category_edit";
	}


	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") long id,
						 @ModelAttribute("category") CategoryForm categoryForm,
						 Model model) {
		Map<String, Object> map = new HashMap<>();
		UserModel userModel = currentUser();
		long store_id = 0;
		if (userModel != null) {
			store_id = userModel.storeId;
		}else {
			map.put("valid", false);
			map.put("errorMsg", "请先登陆");
			return map;
		}

		if(categoryService.existCategory(categoryForm.getName(), id, store_id)){
			map.put("valid", false);
			map.put("errorMsg", categoryForm.getName()+"已存在");
			return map;
		}
		categoryForm.id = id;
		CategoryModel categoryModel = categoryService.updateCategory(categoryForm);

		model.addAttribute("category", categoryModel);
		model.addAttribute("user", currentUser());
		map.put("valid", true);
		map.put("url", "/admin/category");
		return map;
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> delete(@PathVariable("id") long id) {
		CategoryModel categoryModel = categoryService.findCategory(id);
		Map<String, Object> rsp = responseMap();
		if (categoryModel != null) {
			categoryService.delCategory(id);
		} else {
			rsp.put("success", false);
			rsp.put("message", "category doesn't exist");
		}

		return rsp;
	}


	@RequestMapping(value = {"create"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@ModelAttribute("item") CategoryForm categoryForm,
									  Model model) {
		Map<String, Object> map = new HashMap<String, Object>();

		long store_id = 0;
		UserModel userModel = currentUser();
		if (null != userModel) {
			store_id = userModel.storeId;
		}else {
			map.put("valid", false);
			map.put("errorMsg", categoryForm.getName()+"已存在");
			return map;
		}
		model.addAttribute("user", userModel);

		if (categoryService.existCategoryByName(categoryForm.getName(), store_id)) {
			map.put("errorMsg", categoryForm.getName()+"已存在");
			map.put("valid", false);
			return map;
		}

		categoryForm.setStoreId(store_id);
		CategoryModel categoryModel = categoryService.addCategory(categoryForm);

		if (categoryModel.id > 0) {
			map.put("valid", true);
			map.put("url", "/admin/category");
		} else {
			map.put("valid", false);
			map.put("errorMsg", "添加类别失败");
		}
		return map;
	}
}
